package Com.Isman.Adapter;

import java.util.List;

import org.kobjects.base64.Base64;

import Com.Isman.Booking.PageHotelDetail;
import Com.Isman.Booking.R;
import Com.Isman.WebService.ComplexType.Hotel;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class HotelSearchAdapter extends BaseAdapter{
    private final Context context;
    private final List<Hotel> list;

    public HotelSearchAdapter(Context context, List<Hotel> listPhonebook) {
        this.context = context;
        this.list = listPhonebook;
    }

    @Override
	public int getCount() {
        return list.size();
    }
    @Override
	public Object getItem(int position) {
        return list.get(position);
    }
    @Override
	public long getItemId(int position) {
        return position;
    }
    @Override
	public View getView(int position, View convertView, ViewGroup viewGroup) {
        final Hotel entry = list.get(position);
        
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.itemhotellist, null);
        }
        
        //findResource
        TextView hotename 	= (TextView)convertView.findViewById(R.id.lblItemHotelName);
        TextView city 		= (TextView)convertView.findViewById(R.id.lblItemCity);
        TextView price 		= (TextView)convertView.findViewById(R.id.lblItemPrice);
        TextView distance 	= (TextView)convertView.findViewById(R.id.lblItemDistance);
        RatingBar rating 	= (RatingBar)convertView.findViewById(R.id.rtbItemStartRating);
        ImageView imgLogo	= (ImageView)convertView.findViewById(R.id.imgLogo);
        
        
        //Set value. 
        hotename.setText(entry.getHotelName());
        city.setText(entry.getMap().getCity());
        price.setText(entry.getPrice()+" us");
        distance.setText("about 10 km");
        rating.setRating(entry.getHotelRating());
        rating.setIsIndicator(true);
        
        byte[] decodedString = Base64.decode(entry.getLogo().getContent());
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        imgLogo.setImageBitmap(decodedByte);
        
        convertView.setOnClickListener(new  View.OnClickListener() { 
        	@Override
			public void onClick(View v) {
        		Intent intent = new Intent(v.getContext(),PageHotelDetail.class);
        		intent.putExtra(PageHotelDetail.HOTEL_DETAIL, entry);
        		v.getContext().startActivity(intent);
        	}
		});
        return convertView;
    }
}
